layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //用户列表
    var option = {
        elem: '#merchantList',
        url : '/produceComment/page',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "merchantListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'produceName', title: '产品名称', minWidth:200, align:'center',templet:function(d){
                var name = null;
                    $.ajax({
                        url:"/agriecomProduce/"+d.produceId,
                        type:'GET',
                        async:false,
                        success:function (data){
                            name = data.data.produce.produceName;
                        }
                    });
                    return name;
                }},
            {field: 'userId', title: '用户名字', minWidth:100, align:"center"},
            {field: 'commentContent', title: '评论内容',  align:'center'},
            {field: 'thumbsUpCount', title: '点赞数', align:'center'},
            {field: 'commentStatus', title: '评论状态', align:'center',
                templet:function(d){
                    return d.commentStatus == "0" ? "评论可见" : "已禁止";
                }
            },
            {field: 'commentTime', title: '创建时间', align:'center',minWidth:150,sort:true,
                templet:function(d){
                    //处理时间
                    var date = new Date(d.commentTime);
                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                }
            },
            {title: '操作', minWidth:175, templet:'#merchantListBar',fixed:"right",align:"center"}
        ]],
    };

    var tableIns = table.render(option);

    $(".search_btn").on("click",function(){
        var searchKey = $(".searchVal").val();
        if(searchKey != ''){
            option.url = '/produceComment/commentList';
            option.method = 'post';
            option.where = {keyword: searchKey};
            table.render(option);
        }else{
            tableIns.reload();
            // layer.msg("请输入搜索的内容");
        }
    });

    table.on('tool(merchantList)', function(obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'edit') { //编辑
            var index = layui.layer.open({
                title: "修改产品信息",
                type: 2,
                content: "productCommentEdit?id=" + data.id,
                area: ['100%', $(document).height() - 30 + 'px'],
                success: function () {
                    setTimeout(function () {
                        layui.layer.tips('点击此处返回设备列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500);
                }
            });
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize", function () {
                layui.layer.full(index);
            });
        }
        // else if(layEvent === 'check') {
        //
        //     var index = layui.layer.open({
        //         title: "查看产品的评论",
        //         type: 2,
        //         content: "",
        //         area: ['100%', $(document).height() - 30 + 'px'],
        //         success: function () {
        //             setTimeout(function () {
        //                 layui.layer.tips('点击此处返回设备列表', '.layui-layer-setwin .layui-layer-close', {
        //                     tips: 3
        //                 });
        //             }, 500);
        //         }
        //     });
        //     layui.layer.full(index);
        //     //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        //     $(window).on("resize", function () {
        //         layui.layer.full(index);
        //     });
        // }
        else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此用户？',{icon:3, title:'提示信息'},function(index){
                $.get("/produceComment/delete",{
                    id: data.id //将需要删除的Id作为参数传入
                },function(){
                    tableIns.reload();
                    layer.close(index);
                })
            });
        }

    });



    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('merchantListTable'),
            data = checkStatus.data,
            newsId = [];
        if(data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].id);
            }
            layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("/produceComment/batchDelete",{
                     ids : newsId  //将需要删除的newsId作为参数传入
                 },function(data){
                    if (data.code == 200) {
                        layer.msg("删除成功！");
                    } else {
                        layer.msg("删除失败，请重试！");
                    }
                tableIns.reload();
                layer.close(index);
                 })
            })
        }else{
            layer.msg("请选择需要删除的用户");
        }
    })


});